package com.leandom.studydemo.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ViewHolder> {

    private Item[] mDemos;

    public ExampleAdapter(Item[] demos) {
        this.mDemos = demos;
    }

    @NonNull
    @Override
    public ExampleAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
        return new ExampleAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ExampleAdapter.ViewHolder holder, final int position) {
        TextView textView = holder.itemView.findViewById(android.R.id.text1);
        textView.setText(mDemos[position].title);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.itemView.getContext().startActivity(new Intent(holder.itemView.getContext(), mDemos[position].clazz));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDemos.length;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }


    public static class Item {
        public String title;
        public Class clazz;

        public Item(String title, Class clazz) {
            this.title = title;
            this.clazz = clazz;
        }
    }

}